//渲染
function teachershow(){
    $.ajax({
        url:'/teacher/teachershow',
        type:'post',
        success:function(data){
            let total=``;
            data.forEach(e=>{
             total+=` <tr>
             <td>${e._id}</td>
             <td>${e.name}</td>
             <td><button class="but-red teacherdelate-btn">删除</button><button class="but-blue teacherchange-btn">修改</button></td>
         </tr>`
            });
            $(`#teachertbody`).html(total);
        }
    })
}teachershow()
//新增
$(`#teacheradd-btn`).on('click', function(event) {

$.ajax({
    url:'/teacher/teacheradd',
    type:'post',
    data:{
    name:$(this).prev().val()
    },
    success:function(data){
      alert(data.mes);
      teachershow()
    }
})
})
//删除
$(`body`).on('click','.teacherdelate-btn',function(event){
console.log($(this).parent().prev().prev().text());
$.ajax({
    url:'/teacher/teacherdelate',
    type:'post',
    data:{
        _id:$(this).parent().prev().prev().text()
    },
    success: function(data){
        alert(data.mes);
        teachershow()
    }
})
})
//修改
$(`body`).on('click', '.teacherchange-btn',function(event){
   $(this).parent().prev().html(`<input type="text" class="teacher-changeinput"></input>`);
console.log($(this).parent().prev().prev().text());
})
$(`body`).on('blur','.teacher-changeinput',function(event) {
    console.log(555);
reschangeteacher=event.target.parentElement.previousElementSibling.innerText.trim();
console.log(reschangeteacher);
    $.ajax({
        url:'/teacher/teacherchange',
        type:'post',
        data:{
            _id:reschangeteacher,
            name:event.target.value.trim()
        },
        success: function(data){
            alert(data.mes);
            teachershow()
        }
    })
})
    